今日份因時間不足,將今日原訂學習計劃拆成兩日!
Python 中級別類型:List、Sets、Tuple、Dictionary(較複雜下一張講解)
1.List:可輸入重複值!
將多個值儲存到一變數中
fruits = ["apple","orange","banana"]
print(fruit[0])
apple
列出變數中所有值
for f in fruits:
print(f)
apple
orange
banana
列表新增元素
fruit.apped("kiwi")
print(fruits)
列表新增元素
fruit.remove("banana")
print(fruits)
尋找元素索引
print(fruit.index("banana"))
反轉列表
fruit.reverse()
print(fruits)
2.Sets:不能重複且沒有順序的
建立將多個值儲存到一變數中
fruit_set = {"😍","😭","😁"}
for fruit in fruits_set:
print(fruit, end=" ")
😍 😭 😁
判斷元素是否在變數中
if "😍" in fruit_set:
print("There is an apple")
3.Tuple元組:可重複、有順序且不能改變
fruit_tuple = ("😍","😭","😁")
result = trfruit_tuple.count("😍")
print(result)
2
找到元素位子
fruit_tuple.index("😁")
print(result)
2